package src;

abstract class TextComponent {
    public abstract void operation(String outText);
}

// 具体组件类
class SimpleText extends TextComponent {
    @Override
    public void operation(String outText) {
        System.out.println("输出文本：" + outText);
    }
}

// 装饰者抽象类
abstract class TextDecorator extends TextComponent {
    protected TextComponent component;

    public TextDecorator(TextComponent component) {
        this.component = component;
    }

    @Override
    public abstract void operation(String outText);
}

// 具体装饰者类：加粗
class BoldTextDecorator extends TextDecorator {
    public BoldTextDecorator(TextComponent component) {
        super(component);
    }

    @Override
    public void operation(String outText) {
        component.operation(outText);
        addBoldStyle();
    }

    private void addBoldStyle() {
        System.out.println("加粗样式");
    }
}

// 具体装饰者类：斜体
class ItalicTextDecorator extends TextDecorator {
    public ItalicTextDecorator(TextComponent component) {
        super(component);
    }

    @Override
    public void operation(String outText) {
        component.operation(outText);
        addItalicStyle();
    }

    private void addItalicStyle() {
        System.out.println("斜体样式");
    }
}

// 具体装饰者类：下划线
class UnderlineTextDecorator extends TextDecorator {
    public UnderlineTextDecorator(TextComponent component) {
        super(component);
    }

    @Override
    public void operation(String outText) {
        component.operation(outText);
        addUnderlineStyle();
    }

    private void addUnderlineStyle() {
        System.out.println("下划线样式");
    }
}

// 客户端代码
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello and welcome!");

        TextComponent textBox = new SimpleText();

        TextDecorator decoratorBold = new BoldTextDecorator(textBox);

        TextDecorator decoratorItalic = new ItalicTextDecorator(decoratorBold);

        TextDecorator decoratorUnderline = new UnderlineTextDecorator(decoratorItalic);

        decoratorUnderline.operation("你好");

        decoratorBold.operation("第二个你好");

        TextDecorator decorator = new BoldTextDecorator(new ItalicTextDecorator(textBox));
        decorator.operation("第三个你好");
    }
}